package com.vf.admin.client.controller;


import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppGuardService;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppGuard;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.vf.admin.common.Constants.guizuCacheMap;

@CrossOrigin
@Controller
@RequestMapping("/app/guard")
public class AppGuardController extends ClientBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AppGuardService appGuardService;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AppSetupService appSetupService;


    /**
     * 获取我守护的列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/myGuardInfo")
    public Result myGuardInfo()
    {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser appUser = appUserService.get(userId);
            if (StringUtils.isEmpty(appUser)) {
                return this.errorJSON("用户不存在");
            }
            List<AppGuard> myGuardInfo = appGuardService.getMyGuardInfo(appUser.getId());
            return this.sucessJSON(myGuardInfo,"成功");
        }catch (Exception e){
            logger.error("获取我守护的列表异常",e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * 获取守护我的用户
     * @return
     */
    @ResponseBody
    @RequestMapping("/guardForMe")
    public Result guardForMe()
    {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser appUser = appUserService.get(userId);
            if (StringUtils.isEmpty(appUser)) {
                return this.errorJSON("用户不存在");
            }
            AppGuard appGuard = appGuardService.guardForMe(appUser.getId());
            return this.sucessJSON(appGuard,"成功");
        }catch (Exception e){
            logger.error("获取守护我的用户异常",e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }



    /*@ResponseBody
    @RequestMapping("/decIntimacy")
    public Result decIntimacy()
    {
        appGuardService.decIntimacy();
        return this.sucessJSON("success");
    }*/

    /**
     * 查看用户是否有守护
     * @param request
     * @param beiUserId
     * @return
     */
    @ResponseBody
    @RequestMapping("/isGuardInfo")
    public Result isGuardInfo(HttpServletRequest request,Integer beiUserId)
    {
        try{
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser appUser = appUserService.get(userId);
            if (StringUtils.isEmpty(appUser)) {
                return this.errorJSON("用户不存在");
            }
            AppGuard guardInfo = appGuardService.isGuardInfo(beiUserId);
            return this.sucessJSON(guardInfo,"成功");
        }catch (Exception e){
            logger.error("获取我是否有守护",e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * 用户亲密度守护列表
     * @param request
     * @param beiUserId
     * @return
     */
    @ResponseBody
    @RequestMapping("/IntimacyGuardList")
    public Result intimacyGuardList(HttpServletRequest request,Integer beiUserId)
    {
        try{
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser appUser = appUserService.get(userId);
            if (StringUtils.isEmpty(appUser)) {
                return this.errorJSON("用户不存在");
            }
            List<AppGuard> appGuards = appGuardService.intimacyGuardList(beiUserId);
            if (ObjectUtil.isNotEmpty(appGuards)&& appGuards.size()>0){
                for(AppGuard g : appGuards) {
                    AppUser user = appUserService.get(g.getUserId());
                    if (user.getIsguizu() == 1) {
                        String medal = guizuCacheMap.get(user.getNobleid());
                        g.setMedal(medal);
                    } else {
                        g.setMedal("");
                    }
                }
                return this.sucessJSON(appGuards,"成功");
            }

            return this.sucessJSON(null,"成功");
        }catch (Exception e){
            logger.error("查询亲密度守护列表",e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * 获取亲密度列表
     * @param request
     * @param pageno
     * @return
     */
    @ResponseBody
    @RequestMapping("/getIntimacyList")
    public Result getIntimacyList(HttpServletRequest request, Integer pageno)
    {
        Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
        AppUser appUser = appUserService.get(userId);
        if (StringUtils.isEmpty(appUser)) {
            return this.errorJSON("用户不存在");
        }
        if (pageno == null) {
            return this.errorJSON("页码不存在！");
        }
        SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
        List<AppGuard> intimacyList = appGuardService.getIntimacyList(splitPageBean, userId);
        if(intimacyList == null || intimacyList.size()<=0){
            return this.sucessJSON(intimacyList,"成功");
        }
        for(AppGuard g : intimacyList) {
            AppUser user = appUserService.get(g.getUserId());
            if (user.getIsguizu() == 1) {
                String medal = guizuCacheMap.get(user.getNobleid());
                g.setMedal(medal);
            } else {
                g.setMedal("");
            }
        }
        return this.sucessJSON(intimacyList,"成功");
    }

    /**
     * 去守护或顶替守护
     * @param request
     * @param beiUserId
     * @return
     */
    @ResponseBody
    @RequestMapping("/toGuard")
    public Result toGuard(HttpServletRequest request,Integer beiUserId)
    {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser appUser = appUserService.get(userId);
            if (StringUtils.isEmpty(appUser)) {
                return this.errorJSON("用户不存在");
            }
            AppGuard appGuard = appGuardService.toGuard(beiUserId);
            return this.sucessJSON(appGuard,"成功");
        }catch (Exception e){
            logger.error("去守护异常",e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * 获取与对方的亲密度
     * @param request
     * @param toTxCode 对方用户腾讯code
     * @return
     */
    @ResponseBody
    @RequestMapping("/getToUserIntimacy")
    public Result getToUserIntimacy(HttpServletRequest request,Integer toTxCode)
    {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser appUser = appUserService.get(userId);
            if (StringUtils.isEmpty(appUser)) {
                return this.errorJSON("用户不存在");
            }
            AppGuard guaardByUserId = null;
            AppUser tengxuncodeByUser = appUserService.getTengxuncodeByUser(toTxCode);
            if(tengxuncodeByUser != null){
                guaardByUserId = appGuardService.getGuaardByUserId(appUser.getId(), tengxuncodeByUser.getId());

                AppSetup setup = appSetupService.get();
                if(guaardByUserId != null) {
                    guaardByUserId.setIntimacyvoice(setup.getIntimacyvoice());
                }
            }
            return this.sucessJSON(guaardByUserId,"成功");
        }catch (Exception e){
            logger.error("获取与对方的亲密度异常：",e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }






}
